﻿// ***********************************************************************
// Assembly         : OSS.Earth
// Author           : tian_
// Created          : 01-24-2019
//
// Last Modified By : tian_
// Last Modified On : 01-24-2019
// ***********************************************************************
// <copyright file="SlantedImageTestCmd.cs" company="">
//     Copyright ©  2017
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.IO;
using OsgEarth;
using Osg;
using ZFGK.Addins;
using ZFGK.IO;
using ZFGK.OSG.UI;
using ZFGK.OSG.Utility;
using ZFGK.osgEarth.UI;
using ZFGK.Utility;
using ZFGK.WinForms.Utility;

namespace OSS.Earth.Commands.UserTestCmds
{
    /// <summary>
    /// Class SlantedImageTestCmd.
    /// </summary>
    /// <seealso cref="ZFGK.Addins.IExtendCommand" />
    public class SlantedImageTestCmd : IExtendCommand
    {
        /// <summary>
        /// Executes the specified command data.
        /// </summary>
        /// <param name="cmdData">The command data.</param>
        /// <param name="message">The message.</param>
        /// <returns>CmdResult.</returns>
        public CmdResult Execute(ExtendCmdData cmdData, ref string message)
        {
            var form = cmdData.ViewForm as OsgEarthViewForm;
            var osgEarthViewCtrl = form.OsgEarthViewCtrl;
            var actionUtility = osgEarthViewCtrl.ActionUtility;

//             string srcFileName;
//             if (!DialogUtil.OpenOSG(out srcFileName))
//                 return CmdResult.Cancel;
// 
//             string hashCode = FileUtil.GetHashCode(srcFileName);
//             //var node = osgDB.osgDB.readNodeFile(srcFileName);
//             var node = ZfModelCache.GetNode(srcFileName, hashCode);
//             if (!node.IsValid)
//             {
//                 ZfMessageUtil.ShowError("模型读取失败!");
//                 return CmdResult.Cancel;
//             }

            string folder;
            if (!DialogUtil.OpenDir("选择切斜影像父文件夹", null, out folder))
                return CmdResult.Cancel;

            var matrixTransform = new MatrixTransform();
            matrixTransform.getOrCreateStateSet().SetLighting(false);
            foreach (var directory in DirectoryUtil.GetDirectories(folder))
            {
                try
                {
                    string filePath = PathUtil.Combine(directory, Path.GetFileName(directory) + ".osgb");
                    if (!File.Exists(filePath))
                        continue;

                    //string hashCode = FileUtil.GetHashCode(filePath);
                    var node = OsgDB._.readNodeFile(filePath);
                    //var node = ZfModelCache.GetNode(filePath, hashCode);
                    if (!node.IsValid)
                    {
                        ZfMessageUtil.ShowError("模型读取失败!");
                        return CmdResult.Cancel;
                    }

                    matrixTransform.addChild(node);
                }
                catch
                {

                }
            }

            var box = matrixTransform.getBoundBox();
            matrixTransform.setMatrix(Matrixf.translate(-box.GetBottomCenter()));
            matrixTransform.getOrCreateStateSet().SetLighting(false);

            var boxGeode = box.CreateEdgeLine(new Vec4f(1, 0, 0, 1)).CreateGeode();
            boxGeode.getOrCreateStateSet().SetLineWidth(2);
            boxGeode.getOrCreateStateSet().SetLighting(false);

            var mtf1 = new MatrixTransform();
            mtf1.setMatrix(matrixTransform.getMatrix());
            mtf1.addChild(boxGeode);

            //var mtf = new MatrixTransform();
            //mtf.addChild(mtf1);

            mtf1.getOrCreateStateSet().SetLighting(false);

            Vec3d mapPt = null, mapPt2 = null;
            // 选择插入点
            if (!actionUtility.PickEarthPoint("放置模型", mtf1, out mapPt))
                return CmdResult.Cancel;
//             if (!actionUtility.PickEarthPointLastex("旋转模型", "按ESC键退出添加", mapPt, mtf, out mapPt2))
//                 return CmdResult.Cancel;

            //mtf.setMatrix(Matrixd.rotate(mapPt.Rotation(mapPt2), new Vec3d(0, 0, 1)));
            //mtf.removeChild(mtf1);
            //mtf.addChild(matrixTransform);

            GeoTransform geoTransform = new GeoTransform();
            geoTransform.setPosition(new GeoPoint(osgEarthViewCtrl.OsgEarthObj.MapNode.getMapSRS(), mapPt));
            geoTransform.addChild(matrixTransform);

//             OsgNode osgNode = new OsgNode("Models");
//             osgNode.AddNode(new NodeData(srcFileName, geoTransform));

            osgEarthViewCtrl.OsgEarthObj.Viewer.getDatabasePager().registerPagedLODs(matrixTransform);
            osgEarthViewCtrl.OsgEarthObj.AddOrReplaceModel("Models", geoTransform, folder);

            return CmdResult.Succeed;
        }
    }
}
